package com.wqb.server.controller;


import com.wqb.server.pojo.Position;
import com.wqb.server.pojo.RespBean;
import com.wqb.server.service.IPositionService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 职位管理
 * </p>
 *
 * @author 万秋泊
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    @ApiModelProperty(value = "获取所有职位信息")
    @GetMapping("/")
    public List<Position> getAllPosition() {
        return positionService.list();
    }

    @ApiModelProperty(value = "添加所有信息")
    @PostMapping("/addPosition")
    public RespBean addPosition(@RequestBody Position position) {
        //设置创建时间
        position.setCreateDate(LocalDateTime.now());
        if (positionService.save(position)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败，请重试");
    }

    @ApiModelProperty(value = "更新所有信息")
    @PutMapping("/updatePosition")
    public RespBean updatePosition(@RequestBody Position position) {
        if (positionService.updateById(position)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }

    @ApiModelProperty(value = "删除所有信息")
    @DeleteMapping("/deletePosition/{id}")
    public RespBean deletePosition(@PathVariable Integer id) {
        if (positionService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiModelProperty(value = "批量删除所有信息")
    @DeleteMapping("/deletePositions")
    public RespBean deletePositionsByIds(Integer[] ids) {
        if (positionService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
